//
//  MTWorksApiManager.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/6.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTWorksApiManager.h"
#import "YTKKeyValueStore.h"
@interface MTWorksApiManager ()
@property(nonatomic,strong) YTKKeyValueStore *store;

@end

static MTWorksApiManager *worksmanger = nil;
@implementation MTWorksApiManager

+(MTWorksApiManager *)manager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        worksmanger = [[MTWorksApiManager alloc] init];
    });
    return worksmanger;
}

-(YTKKeyValueStore *)store
{
    if(_store == nil)
    {
        _store = [[YTKKeyValueStore alloc] initDBWithName:MiTuCacheDBName];
        [_store createTableWithName:HomeTableName];
    }
    return _store;
}


//获取首页缓存

-(NSMutableArray<MTBannerModel *> *)getCacheBanners
{
    NSMutableArray *datas = [self.store getObjectById:BannerKey fromTable:HomeTableName];
    NSMutableArray *banners = [MTBannerModel mj_objectArrayWithKeyValuesArray:datas];
    return banners;
}







/**
 获取banner
 
 @param completionBlock 完成block
 */
-(void)getBannersWithcompletion:(void (^)(NSMutableArray<MTBannerModel *> *banner ,MTError *mterror))completionBlock
{
    [[MTHttpTool shareTool] getPath:@"/ads/banner" parameters:nil success:^(MTBaseData *datamodel) {
        NSMutableArray *bannerArray = [MTBannerModel mj_objectArrayWithKeyValuesArray:datamodel.data];
        [self.store putObject:datamodel.data withId:BannerKey intoTable:HomeTableName];
        completionBlock(bannerArray,nil);
    } failer:^(MTError *error) {
         completionBlock(nil,error);
    }];
}

/**
 获取用户屏蔽的user数组对象
 
 @param completionBlock 完成block
 */
-(void)getShieldUsersWithcomplation:(void (^)(NSMutableArray<MTUserModel *> *shieldUsers, MTError *error))completionBlock
{
    [[MTHttpTool shareTool] getPath:@"/work/get_shield_users" parameters:nil success:^(MTBaseData *datamodel){
        NSMutableArray *userArray = [MTUserModel mj_objectArrayWithKeyValuesArray:datamodel.data];
        completionBlock(userArray,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
}





/**
 移除用户屏蔽
 
 @param userId userId
 @param completionBlock 完成block
 */
-(void)removeShieldUser:(NSString *)userId
             completion:(void (^)(MTError *error))completionBlock
{
    NSDictionary *param = @{@"userId":userId};
    [[MTHttpTool shareTool] postPath:@"/work/remove_shield_user" parameters:param success:^(MTBaseData *datamodel){
        completionBlock(nil);
    } failer:completionBlock];
    
}


/**
 移除作品屏蔽
 
 @param workId 作品ID
 @param completionBlock 完成block
 */
-(void)removeShieldWork:(NSString *)workId
             completion:(void (^)(MTError *boerror))completionBlock
{
    NSDictionary *param = @{@"workId":workId};
    [[MTHttpTool shareTool] postPath:@"/work/remove_shield_work" parameters:param success:^(MTBaseData *datamodel){
        completionBlock(nil);
    }failer:completionBlock];
}

@end
